/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

import java.util.Collection;

import com.eelpo.common.util.BlankUtils;

/**
 * <pre>
 * 
 * Between表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class BetweenExpression implements ParameterCriterion {

	private boolean useAlias;
	private String operator;
	private String propertyName;
	private Object before;
	private Object after;

	private BetweenExpression(String operator, String propertyName, boolean useAlias, Object before, Object after) {
		this.before = before;
		this.after = after;
		this.operator = operator;
		this.propertyName = propertyName;
		this.useAlias = useAlias;
	}

	public static BetweenExpression getBetweenExpression(String propertyName, Object before, Object after) {
		if (BlankUtils.isBlank(before) || BlankUtils.isBlank(after)) {
			return null;
		}
		return new BetweenExpression(BETWEEN, propertyName, true, before, after);
	}

	public static BetweenExpression getNotBetweenExpression(String propertyName, Object before, Object after) {
		if (BlankUtils.isBlank(before) || BlankUtils.isBlank(after)) {
			return null;
		}
		return new BetweenExpression(NOTBETWEEN, propertyName, true, before, after);
	}

	public static BetweenExpression getBetweenInornateExpression(String propertyName, Object before, Object after) {
		if (BlankUtils.isBlank(before) || BlankUtils.isBlank(after)) {
			return null;
		}
		return new BetweenExpression(BETWEEN, propertyName, false, before, after);
	}

	public static BetweenExpression getNotBetweenInornateExpression(String propertyName, Object before, Object after) {
		if (BlankUtils.isBlank(before) || BlankUtils.isBlank(after)) {
			return null;
		}
		return new BetweenExpression(NOTBETWEEN, propertyName, false, before, after);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getJpql(java.lang.
	 *      StringBuilder)
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String alias) {
		jpql.append(AND);
		if (useAlias) {
			jpql.append(alias);
		}
		jpql.append(propertyName);
		jpql.append(operator);
		return jpql;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getParameters(java.util.
	 *      Collection)
	 */
	@Override
	public Collection<Object> getParameters(Collection<Object> parameters) {
		parameters.add(before);
		parameters.add(after);
		return parameters;
	}
}
